<?php

/**
	Theme Validation Library

	The contents of this file are subject to the terms of the GNU General
	Public License Version 2.0. You may not use this file except in
	compliance with the license. Any of the license terms and conditions
	can be waived if you get permission from the copyright holder.

	Copyright (c) 2011 Bandit Media
	Jermaine Maree

		@package BanditValidate
		@version 1.1
**/

//! BanditValidate
class BanditValidate extends FeatherTheme {

	/**
		Initialize
	**/
	static function init($input) {
		// Get current options
		$valid = self::$option;
		// Get tab
		$tab = esc_attr($input['tab']);
		unset($input['tab']);
		// Validate tab
		switch ($tab) {
			case 'theme':
				$valid = self::theme($input,$valid);
				break;
			case 'portfolio':
				$valid = self::portfolio($input,$valid);
				break;
			case 'general':
				$valid = self::general($input,$valid);
				break;
		}
		// Add settings notice
		add_settings_error('feather_setting_notices',
			'feather-updated',__('Settings saved.','creatic'),'updated');
		// Return validated options
		return $valid;
	}

	/**
		Theme
			@return array
			@param $input array
			@param $valid array
			@private
	**/
	private static function theme($input,$valid) {
		// Checkbox options
		$checkboxes = 'theme_disable_author|theme_blog_ajaxnav|footer_widgets|footer_widgets_portfolio';
		// Validate checkbox options
		foreach(explode('|',$checkboxes) as $option) 
			$valid[$option] = isset($input[$option])?'1':'0';
		// Theme Style
		$valid['theme_style']=esc_attr($input['theme_style']);
		// Sidebar Links
		$valid['theme_sidebar_num']=in_array($input['theme_sidebar_num'],range(0,10))?$input['theme_sidebar_num']:'0';
		// Sidebar Link Items
		if($valid['theme_sidebar_num']) {
			$tmp='text|icon|link|custom';
			for($i=1;$i<=$valid['theme_sidebar_num'];$i++) {
				foreach(explode('|',$tmp) as $item) {
					if(isset($input['theme_sidebar_'.$item.$i]))
						$valid['theme_sidebar_'.$item.$i]=wp_filter_nohtml_kses($input['theme_sidebar_'.$item.$i]);
					else
						$valid['theme_sidebar_'.$item.$i]='';
				}
			}
			unset($tmp);
		}
		// Return validated options
		return $valid;
	}

	/**
		Portfolio
			@return array
			@param $input array
			@param $valid array
			@private
	**/
	private static function portfolio($input,$valid) {
		// Checkbox options
		$checkboxes = 'theme_portfolio|theme_portfolio_ajaxnav';
		// Validate checkbox options
		foreach(explode('|',$checkboxes) as $option) 
			$valid[$option] = isset($input[$option])?'1':'0';
		// Rewrite URL's
		$valid['theme_portfolio_rewrite_type'] = esc_attr($input['theme_portfolio_rewrite_type']);
		$valid['theme_portfolio_rewrite_category'] = esc_attr($input['theme_portfolio_rewrite_category']);
		// Portfolio Categories
		if($valid['theme_portfolio']) {
			// Get portfolio categories
			$port_cats=get_categories(array('taxonomy'=>'portfolio_category','orderby'=>'name','hide_empty'=>0));
			foreach($port_cats as $category) {
				if(isset($category->slug)) {
					$option_name='theme_portfolio_'.$category->slug;
					$valid[$option_name]=in_array($input[$option_name],range(1,12))?$input[$option_name]:'1';
					$valid[$option_name.'_num']=in_array($input[$option_name.'_num'],range(1,40))?$input[$option_name.'_num']:'0';
					$valid[$option_name.'_lightbox']=isset($input[$option_name.'_lightbox'])?'1':'0';
				}
			}
			// Flush rewrite rules
			flush_rewrite_rules();
		}
		// Return validated options
		return $valid;
	}

	/**
		General
			@return array
			@param $input array
			@param $valid array
			@private
	**/
	private static function general($input,$valid) {
		// Checkbox options
		$checkboxes = 'disable_tagline|custom_styles|'.
			'comments_pages|comments_posts|analytics_location';
		// Validate checkbox options
		foreach(explode('|',$checkboxes) as $option) 
			$valid[$option] = isset($input[$option])?'1':'0';
		// Custom Logo
		$valid['custom_logo'] = esc_url($input['custom_logo']);
		// Feed URL
		$valid['feed_url'] = esc_url($input['feed_url']);
		// Blog Content
		$valid['blog_index_content'] = ($input['blog_index_content']=='content')?'content':'excerpt';
		// Analytics Location
		$valid['analytics_location'] = ('header'==$input['analytics_location'])?'header':'footer';
		// Analytics Script
		$valid['analytics_script'] = $input['analytics_script'];
		// Return validated options
		return $valid;
	}

	/**
		Language
			@return array
			@param $input array
			@param $valid array
			@private
	**/
	static function language($input) {
		// Get current options
		$valid = self::$lang;
		// Get language settings
		$settings = FeatherConfig::load('language','lang');
		unset($settings['sections']);
		// Loop through language settings
		foreach($settings as $lang) {
			$id = $lang['id'];
			$valid[$id] = wp_filter_kses($input[$id]);
		}
		// Add settings notice
		add_settings_error('feather_setting_notices',
			'feather-updated',__('Settings saved.','creatic'),'updated');
		// Return validated options
		return $valid;
	}

}
